/*
 * GUI.java
 *  This file is part of Spidex.
 *  
 *  Spidex is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  Spidex is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Spidex.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package spidex;

import WindowLogger.WindowLogger;
import WindowLogger.WindowLoggerHandler;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.logging.Logger;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.Vector;
import java.util.logging.Level;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableRowSorter;
import spidex.dbmanager.ConditionType;
import spidex.dbmanager.DBCondition;
import spidex.dbmanager.DBManager;
import spidex.dbmanager.DBSQLite;
import spidex.importer.ColumnType;
import spidex.importer.ImportGUI;
import spidex.posterSearcher.PosterDialog;
import spidex.resources.UserDirManager;
import spidex.table.ColumntTypeRowFilter;
import spidex.table.DVDInfoTableModel;
import spidex.table.ImageResizer;
import spidex.version.VersionDialog;

/**
 *
 * @author  xatrix
 */
public class GUI extends javax.swing.JFrame {
    
    /** Creates new form GUI */
    @SuppressWarnings("static-access")
    public GUI() {
        setUpGUI();
        FillTable();
    }
    
    /**
     * Function checks whether exists directory with user settings and posters, 
     * also set table properities
     */    
    private void setUpGUI() {
        
        try {
             userSettings = UserDirManager.getInstance().checkUserSettings();
        }
        catch(IOException ex) {
            logger.log(Level.SEVERE, ex.getMessage());
//        TODO erase
//            System.out.println(ex.getMessage());
            System.exit(1);
        }
        initComponents();
        
        this.table.setRowHeight(
            Integer.parseInt( ResourceBundle.getBundle("spidex/properties").
                getString("posterSmallHeight")
            )
        );
        TableColumn column = table.getColumnModel().getColumn(1);
        column.setMaxWidth(
            Integer.parseInt( ResourceBundle.getBundle("spidex/properties").
                getString("posterSmallWidth")
            )
        );
        column = table.getColumnModel().getColumn(0);
        column.setMaxWidth(20);
        column.setResizable(true);
        column = table.getColumnModel().getColumn(2);
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        renderer.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
        column.setCellRenderer( renderer );
    }
    
    /**
     * Function only set data from GUI textfields to actual info record
     */
    private void synchronizeData() {
        String directorStringValue;
        String[] directors;
        String actors;
        
        this.info.setTitle( nameField.getText() );
        this.info.setGenre( genreField.getText() );
        this.info.setRating( ratingField.getText()  );
        this.info.setYearOfRelease( yearField.getText() );
        this.info.setLength( lengthField.getText()  );
        this.info.setBorrowed( borrowCheckBox.isEnabled() );
        this.info.setBorrowedFromWho( borrowesToField.getText());
        this.info.setRented( lendCheckBox.isEnabled() );
        this.info.setRentedToWho( lendFromField.getText() );
        this.info.setType( typeComboBox.getSelectedIndex() );
        this.info.setCompilation( compilationField.getText() );
        
        String filmInfo = infoTextArea.getText();
        if(filmInfo.length() > 180) {
            filmInfo.substring(0, 179);
        }
        this.info.setInfo( filmInfo );
//        directorStringValue = directorField.getText();
//        System.out.println("Directors:"+directorStringValue);
//        directors = directorStringValue.split(", ");
//        for(String director : directors) {
//            this.info.addDirector(director);
//        }
    }
    
    private void setUpColumnWidth(int rowId) {
        int size;
        TableColumn column = table.getColumnModel().getColumn(0);
        
        for(int index=1; ;index++) {
            if(rowId < Math.pow(10,index) ) {
                size = index*10;
                break;
            }
        }
        column.setMaxWidth( size );
        column.setMinWidth( size );
        column.setPreferredWidth( size );
        column.setWidth(size);

    }
    
    /**
     * Function open database and insert existing DVDInfo records into table
     */
    private void FillTable() {
        DVDInfo resultInfo = null;
        ImageIcon recordIcon = null;
        sorter = new TableRowSorter<DefaultTableModel>(this.model);
        this.table.setRowSorter(sorter);
        
        try {
            this.dbmanager.openDb(userSettings.get("database"));
            result = this.dbmanager.findEntries( 
                new DBCondition(ConditionType.FILMNAME,"%")
            );
            
        }
        catch(ClassNotFoundException ex) {
            logger.log(Level.SEVERE,ex.getMessage());
        }
        catch(SQLException ex) {
            logger.log(Level.SEVERE,ex.getMessage());
//        TODO erase
//            System.out.println(ex.getMessage()+"|"+ex.toString());
        }
        if(result == null) {
            logger.log(Level.SEVERE,"Result je null");
            return;
        }
        else {
            logger.log(Level.SEVERE,"Pocet prvku je "+result.size());
        }
        DefaultTableModel tableModel = (DefaultTableModel) this.table.getModel();
        
        for(Iterator<DVDInfo> iterator = result.iterator();
            iterator.hasNext();) {
            resultInfo = (DVDInfo) iterator.next();
            logger.log(Level.SEVERE,resultInfo.toString());
            recordIcon = new ImageIcon(
                userSettings.get("posterdir")+resultInfo.getId()+".jpg");
            if(recordIcon.getIconWidth() < 1) {
                recordIcon = new ImageIcon(
                    getClass().getResource("/spidex/resources/not_avail.jpg")
                );
            }
            resultInfo.setPoster(recordIcon);
            this.addTableRecord(tableModel,resultInfo);
        }
        
        //Set width of ID column by number of numbers in last entry
        TableColumn column = table.getColumnModel().getColumn(0);
        int size;
        if(resultInfo != null) {
            setUpColumnWidth(resultInfo.getId());
        }

    }
    
    /**
     * Function set actual data from param info to GUI textLabels and set 
     * checkBoxes if necessary
     * @param info
     */
    public void updateGUIInformation(DVDInfo info) {
        this.info = info;
        this.posterLabel.setIcon( this.info.getPoster() );
        this.nameField.setText( this.info.getTitle() );
        this.directorField.setText( this.info.toStringDirectors() );
        this.castField.setText( this.info.toStringActors() );
        this.genreField.setText( this.info.getGenre() );
        this.yearField.setText( String.valueOf( this.info.getYearOfRelease() ));
        this.ratingField.setText(String.valueOf( this.info.getRating() ) );
        this.lengthField.setText(String.valueOf( this.info.getLength() )+"min");
        if(this.info.getBorrowed() ) {
            this.borrowCheckBox.setSelected(true);
            this.borrowesToField.setText( info.getBorrowedFromWho() );
        }
        else {
            this.borrowCheckBox.setSelected(false);
            this.borrowesToField.setText( null );
        }
        if(this.info.getRented()) {
            this.lendCheckBox.setSelected(true);
            this.lendFromField.setText( info.getRentedToWho() );
        }
        else {
            this.lendCheckBox.setSelected(false);
            this.lendFromField.setText( null );
        }
        this.compilationField.setText( info.getCompilation() );
        this.typeComboBox.setSelectedIndex(info.getType().getInt());
        this.infoTextArea.setText( this.info.getInfo() );
        
    }

    public void addImportRecords(Set<DVDInfo> records) {
        for(DVDInfo record : records) {
            this.insertInfo(record);
        }
        this.setUpColumnWidth(this.info.getId());
    }
    
    /**
     * Function insert DVDInfo class into Jtable model and also resize Poster
     * @param model TableModel for inserting rows
     * @param record inserting information
     */
    private void addTableRecord (DefaultTableModel model, DVDInfo record) {
            if(record == null) {
                throw new NullPointerException("record");
            }
            Vector vector = new Vector();
            vector.add( String.valueOf( record.getId() ) );
            vector.add(ImageResizer.getInstance().resize( record.getPoster() ));
            vector.add( record.getTitle() );
            model.addRow(vector);
    }
    
    private void insertInfo( DVDInfo info) {
        if(info == null) {
            return;
        }
        this.info = info;
        try {
            logger.log(Level.INFO,
                info.getId() +"|"+this.dbmanager.existEntry( info.getId() ) 
            );
            
            if(info.getId() > 0 || this.dbmanager.existEntry( info.getId() ) ) {
                logger.log(Level.INFO, "Updating record "+info.getId());
                this.dbmanager.updateRecord(info);
                int rowId = this.table.getSelectedRow();
                try {
                    rowId = table.convertRowIndexToModel(rowId);
                }
                catch (IndexOutOfBoundsException ex) {
                    return;
                }
                this.result.set(rowId, this.info);
                this.updateGUIInformation( this.result.get(rowId) );
                this.info.savePoster(
                    userSettings.get("posterdir")+info.getId()+".jpg"
                );
                return;
            }
            
            int id = this.dbmanager.insertRecord( info );
            info.setId(id);
            result.add(info);
            this.addTableRecord(
                    (DefaultTableModel) this.table.getModel(), this.info 
            );
            this.info.savePoster(
                userSettings.get("posterdir")+info.getId()+".jpg"
            );
        }
        catch(IOException ex) {
            logger.log(Level.SEVERE, 
                    "IOException during saving posters!: "+ex.getMessage()
            );
        }
        catch(SQLException ex) {
            logger.log(Level.SEVERE,ex.getMessage());
//        TODO erase
//            System.out.println(ex.getMessage());
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fileChooser = new javax.swing.JFileChooser();
        tablePopup = new javax.swing.JPopupMenu();
        selectAllItem = new javax.swing.JMenuItem();
        updatePoster = new javax.swing.JMenuItem();
        popUpSeparator = new javax.swing.JSeparator();
        eraseItem = new javax.swing.JMenuItem();
        scrollPane = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();
        searchField = new javax.swing.JTextField();
        filterLabel = new javax.swing.JLabel();
        posterLabel = new javax.swing.JLabel();
        nameField = new javax.swing.JTextField();
        directorField = new javax.swing.JTextField();
        castField = new javax.swing.JTextField();
        genreField = new javax.swing.JTextField();
        yearField = new javax.swing.JTextField();
        ratingField = new javax.swing.JTextField();
        nameLabel = new javax.swing.JLabel();
        directorLabel = new javax.swing.JLabel();
        castLabel = new javax.swing.JLabel();
        genreLabel = new javax.swing.JLabel();
        yearLabel = new javax.swing.JLabel();
        ratingLabel = new javax.swing.JLabel();
        clearButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();
        separator = new javax.swing.JSeparator();
        borrowCheckBox = new javax.swing.JCheckBox();
        borrowesToField = new javax.swing.JTextField();
        lendCheckBox = new javax.swing.JCheckBox();
        lendFromField = new javax.swing.JTextField();
        lengthLabel = new javax.swing.JLabel();
        lengthField = new javax.swing.JTextField();
        typeLabel = new javax.swing.JLabel();
        typeComboBox = new javax.swing.JComboBox();
        infoPane = new javax.swing.JScrollPane();
        infoTextArea = new javax.swing.JTextArea();
        compilationField = new javax.swing.JTextField();
        compilationLabel = new javax.swing.JLabel();
        clearSearchFieldButton = new javax.swing.JButton();
        filterTypeComboBox = new javax.swing.JComboBox();
        menuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        findFilmItem = new javax.swing.JMenuItem();
        exitItem = new javax.swing.JMenuItem();
        settingsItem = new javax.swing.JMenu();
        debugWindowItem = new javax.swing.JMenuItem();
        importItem = new javax.swing.JMenuItem();
        versionItem = new javax.swing.JMenuItem();

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("spidex/properties"); // NOI18N
        selectAllItem.setText(bundle.getString("selectAll")); // NOI18N
        selectAllItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectAllItemActionPerformed(evt);
            }
        });
        tablePopup.add(selectAllItem);

        updatePoster.setText(bundle.getString("updatePoster")); // NOI18N
        updatePoster.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updatePosterActionPerformed(evt);
            }
        });
        tablePopup.add(updatePoster);
        tablePopup.add(popUpSeparator);

        eraseItem.setText(bundle.getString("eraseItem")); // NOI18N
        eraseItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eraseItemActionPerformed(evt);
            }
        });
        tablePopup.add(eraseItem);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Spidex");

        table.setAutoCreateRowSorter(true);
        table.setModel(model);
        table.setComponentPopupMenu(tablePopup);
        table.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableMouseClicked(evt);
            }
        });
        scrollPane.setViewportView(table);

        searchField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                searchFieldKeyPressed(evt);
            }
        });

        filterLabel.setText("Filter");

        posterLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/spidex/resources/not_avail.jpg"))); // NOI18N
        posterLabel.setToolTipText(bundle.getString("posterHint")); // NOI18N
        posterLabel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        posterLabel.setMaximumSize(new java.awt.Dimension(131, 180));
        posterLabel.setMinimumSize(new java.awt.Dimension(131, 180));
        posterLabel.setPreferredSize(new java.awt.Dimension(125, 180));
        posterLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                posterLabelClicked(evt);
            }
        });

        nameField.setMinimumSize(new java.awt.Dimension(195, 19));

        directorField.setMinimumSize(new java.awt.Dimension(195, 19));

        castField.setMinimumSize(new java.awt.Dimension(195, 19));

        genreField.setMinimumSize(new java.awt.Dimension(195, 19));

        yearField.setMinimumSize(new java.awt.Dimension(195, 19));

        ratingField.setMinimumSize(new java.awt.Dimension(195, 19));

        nameLabel.setText(bundle.getString("name")); // NOI18N
        nameLabel.setPreferredSize(new java.awt.Dimension(80, 15));

        directorLabel.setText(bundle.getString("directed")); // NOI18N
        directorLabel.setPreferredSize(new java.awt.Dimension(80, 15));

        castLabel.setText(bundle.getString("cast")); // NOI18N
        castLabel.setPreferredSize(new java.awt.Dimension(80, 15));

        genreLabel.setText(bundle.getString("genre")); // NOI18N
        genreLabel.setPreferredSize(new java.awt.Dimension(80, 15));

        yearLabel.setText(bundle.getString("year")); // NOI18N

        ratingLabel.setText(bundle.getString("rating")); // NOI18N

        clearButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/spidex/resources/clearButton.png"))); // NOI18N
        clearButton.setText(bundle.getString("clear")); // NOI18N
        clearButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearButtonActionPerformed(evt);
            }
        });

        saveButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/spidex/resources/saveButton.png"))); // NOI18N
        saveButton.setText(bundle.getString("save")); // NOI18N
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        borrowCheckBox.setText(bundle.getString("borrowed")); // NOI18N
        borrowCheckBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                borrowLendCheckBoxStateChanged(evt);
            }
        });

        borrowesToField.setEditable(false);

        lendCheckBox.setText(bundle.getString("lend")); // NOI18N
        lendCheckBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                borrowLendCheckBoxStateChanged(evt);
            }
        });

        lendFromField.setEditable(false);

        lengthLabel.setText(bundle.getString("length")); // NOI18N
        lengthLabel.setPreferredSize(new java.awt.Dimension(80, 15));

        lengthField.setMinimumSize(new java.awt.Dimension(195, 19));

        typeLabel.setText(bundle.getString("type")); // NOI18N

        typeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "DVD", "Blu-ray", "HD-DVD", "VHS", "Other" }));

        infoTextArea.setColumns(20);
        infoTextArea.setRows(5);
        infoTextArea.setToolTipText(bundle.getString("infoHint")); // NOI18N
        infoPane.setViewportView(infoTextArea);

        compilationLabel.setText(bundle.getString("compilation")); // NOI18N

        clearSearchFieldButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/spidex/resources/clearButton.png"))); // NOI18N
        clearSearchFieldButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearSearchFieldButtonActionPerformed(evt);
            }
        });

        filterTypeComboBox.setModel(new javax.swing.DefaultComboBoxModel(columns));

        menuBar.setAutoscrolls(true);

        fileMenu.setText(bundle.getString("file")); // NOI18N

        findFilmItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F, java.awt.event.InputEvent.CTRL_MASK));
        findFilmItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/spidex/resources/findFilmButton.png"))); // NOI18N
        findFilmItem.setText(bundle.getString("findFilm")); // NOI18N
        findFilmItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findFilmItemActionPerformed(evt);
            }
        });
        fileMenu.add(findFilmItem);

        exitItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/spidex/resources/cancelButton.png"))); // NOI18N
        exitItem.setText(bundle.getString("exit")); // NOI18N
        exitItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitItemActionPerformed(evt);
            }
        });
        fileMenu.add(exitItem);

        menuBar.add(fileMenu);

        settingsItem.setText(bundle.getString("settings")); // NOI18N

        debugWindowItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/spidex/resources/debugButton.png"))); // NOI18N
        debugWindowItem.setText(bundle.getString("debugLogger")); // NOI18N
        debugWindowItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                debugWindowItemActionPerformed(evt);
            }
        });
        settingsItem.add(debugWindowItem);

        importItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_I, java.awt.event.InputEvent.CTRL_MASK));
        importItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/spidex/resources/importButton.png"))); // NOI18N
        importItem.setText(bundle.getString("import")); // NOI18N
        importItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                importItemActionPerformed(evt);
            }
        });
        settingsItem.add(importItem);

        versionItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/spidex/resources/versionButton.png"))); // NOI18N
        versionItem.setText(bundle.getString("version")); // NOI18N
        versionItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                versionItemActionPerformed(evt);
            }
        });
        settingsItem.add(versionItem);

        menuBar.add(settingsItem);

        setJMenuBar(menuBar);
        menuBar.getAccessibleContext().setAccessibleParent(this);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(filterLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(searchField, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(filterTypeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(clearSearchFieldButton, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(scrollPane, javax.swing.GroupLayout.Alignment.TRAILING, 0, 0, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lengthLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lengthField, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(posterLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(castLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(castField, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                                            .addComponent(directorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(nameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(directorField, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                                            .addComponent(ratingLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(yearLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(yearField, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(ratingField, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(genreLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(genreField, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(145, 145, 145))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(116, 116, 116)
                        .addComponent(clearButton)
                        .addGap(38, 38, 38)
                        .addComponent(saveButton))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(separator, javax.swing.GroupLayout.PREFERRED_SIZE, 427, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(borrowCheckBox)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(borrowesToField, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(39, 39, 39)
                                        .addComponent(lendCheckBox)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(lendFromField, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(compilationLabel)
                                            .addComponent(typeLabel))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(typeComboBox, 0, 75, Short.MAX_VALUE)
                                            .addComponent(compilationField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE))
                                        .addGap(12, 12, 12)
                                        .addComponent(infoPane, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)))
                                .addGap(27, 27, 27)))))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {borrowesToField, lendFromField});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {lengthLabel, ratingLabel});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(filterLabel)
                                .addComponent(searchField, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(filterTypeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(clearSearchFieldButton))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(161, 161, 161)
                                .addComponent(separator, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(borrowCheckBox)
                                    .addComponent(borrowesToField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(lendCheckBox)
                                    .addComponent(lendFromField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                            .addComponent(typeLabel)
                                            .addComponent(typeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                            .addComponent(compilationLabel)
                                            .addComponent(compilationField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGap(95, 95, 95))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(infoPane)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(clearButton)
                                    .addComponent(saveButton))
                                .addGap(6, 6, 6))
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE)
                                .addContainerGap())))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(nameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(directorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(directorField, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(4, 4, 4)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(castLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(castField, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(genreLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(genreField, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(yearLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(yearField, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(ratingLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(ratingField, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(lengthField, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(lengthLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addComponent(posterLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE))))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {castField, castLabel, directorField, directorLabel, genreField, genreLabel, lengthField, lengthLabel, nameField, nameLabel, ratingField, ratingLabel, yearField, yearLabel});

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {clearSearchFieldButton, filterTypeComboBox, searchField});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Function gets selected row from table and execute updating GUI
     * @param evt event representing what happened
     */
    private void tableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableMouseClicked
        int rowId = this.table.getSelectedRow();
        try {
            rowId = table.convertRowIndexToModel(rowId);
        }
        catch (IndexOutOfBoundsException ex) {
            return;
        }
        if(rowId < 0) {
            return;
        }
        
        this.updateGUIInformation( this.result.get(rowId) );
}//GEN-LAST:event_tableMouseClicked

    /**
     * First function execute GUI and real data synchronization and then 
     * query DB whether film entry exists, if so program triggers updating of
     * query, othervise insert new entry to DB, store poster image and add 
     * record to table.
     * 
     * @param evt event representing what happened
     */
    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        this.synchronizeData();
        this.insertInfo(this.info);
        this.setUpColumnWidth(this.info.getId());
    }//GEN-LAST:event_saveButtonActionPerformed

    private void exitMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuActionPerformed
        try {
            this.dbmanager.close();
        } catch (SQLException ex) {
            Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, ex.getMessage(), ex.getSQLState());
        }
        System.exit(0);
}//GEN-LAST:event_exitMenuActionPerformed

    private void debugMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_debugMenuActionPerformed
        if( this.windowLogger == null) {
            windowLogger = new WindowLogger();
        }
        windowLogger.setVisible(true);
        this.logger = Logger.getLogger("Spidex");
        logger.addHandler( WindowLoggerHandler.getInstance() );
}//GEN-LAST:event_debugMenuActionPerformed
    /**
     * Function ask whether user really want to delete selected entry and then
     * execute delete procedure from DBManager
     * 
     * @param evt
     */
    private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearButtonActionPerformed
        int questionResult = JOptionPane.showConfirmDialog(
               this, ResourceBundle.getBundle("spidex/properties").
                   getString("clearQuestion")
               );

        if(questionResult >= 1) {
           return;
        }
        
//        int rowID = table.getSelectedRow();
//        System.out.println("-----|"+rowID+"|-------");
//        while( rowID >= 0 ) {
//            this.eraseRecord(rowID);
//            rowID = table.getSelectedRow();
//            System.out.println("-----|"+rowID+"|-------");
//        }
//        System.out.println("END-----|"+rowID+"|-------END");
        int[] selectedRows = table.getSelectedRows();
        Arrays.sort(selectedRows);
        for(int rowID=selectedRows.length-1; rowID >= 0; rowID--) {
            this.eraseRecord(selectedRows[rowID]);
        }
        
        
    }//GEN-LAST:event_clearButtonActionPerformed

    private void eraseRecord(int rowId) {
        try {
            rowId = table.convertRowIndexToModel(rowId);
            System.out.println("Erasing---"+rowId);
        }
        catch (IndexOutOfBoundsException ex) {
            return;
        }
        if(rowId < 0) {
            return;
        }

        try {
            this.dbmanager.deleteRecord(this.result.get(rowId));
        }
        catch(SQLException ex) {
            logger.log(Level.SEVERE, 
                    "IOException during saving posters!: "+ex.getMessage()
            );
        }

        this.result.remove(rowId);
        this.model.removeRow(rowId);
    }
private void borrowLendCheckBoxStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_borrowLendCheckBoxStateChanged
    JCheckBox changed;
    JTextField changedField;
    JCheckBox other;
    JTextField otherField;
    if( this.borrowCheckBox.equals( evt.getItem() ) ) {
        changed = this.borrowCheckBox;
        changedField = this.borrowesToField;
        other = this.lendCheckBox;
        otherField = this.lendFromField;
    }
    else {
        other = this.borrowCheckBox;
        otherField = this.borrowesToField;
        changed = this.lendCheckBox;
        changedField = this.lendFromField;
    }
    
    if(changed.isSelected()) {
        changedField.setEditable(true);
        if(other.isSelected()) {
            other.setSelected(false);
            otherField.setText(null);
            otherField.setEditable(false);
        }
    }
    else {
        changedField.setEditable(false);
        changedField.setText(null);
    }
    
}//GEN-LAST:event_borrowLendCheckBoxStateChanged

private void clearSearchFieldButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearSearchFieldButtonActionPerformed
    this.searchField.setText("");
    sorter.setRowFilter(null);
}//GEN-LAST:event_clearSearchFieldButtonActionPerformed

private void versionMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_versionMenuActionPerformed
        if (versionDialog == null) {
            versionDialog = new VersionDialog(this, false);
        }
        versionDialog.setVisible(true);
}//GEN-LAST:event_versionMenuActionPerformed

private void searchFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_searchFieldKeyPressed
if( KeyEvent.VK_ENTER == evt.getKeyCode() ) {
        String searchText = this.searchField.getText();
        if(searchText == null) {
            return;
        }
//        RowFilter<DefaultTableModel, Object> rf = null;
        
//        RowFilter<DefaultTableModel, Object> rf = new RowFilter<DefaultTableModel, Object>() {
//            public boolean include(Entry<? extends DefaultTableModel, ? extends Object> entry) {
//                int identifier = (Integer) entry.getIdentifier();
//                String matchValue = searchText;
//                DefaultTableModel tableModel = entry.getModel();
//                System.out.println(tableModel.getValueAt(identifier, 2));
//                return result.get(identifier).getCompilation().startsWith(matchValue);
//                
//             return true;
//            }
//        };


    //If current expression doesn't parse, don't update.
//        try {
//            rf = RowFilter.regexFilter(searchText, 2);
//        } catch (java.util.regex.PatternSyntaxException e) {
//            return;
//        }
        //Select actual choosed type from Combo Box
        ColumnType type = ColumnType.getType(
            this.filterTypeComboBox.getSelectedIndex()+1
        );
        
        //Create chosen Filter filtering by type
        RowFilter<DefaultTableModel, Object> filter = 
            new ColumntTypeRowFilter<DefaultTableModel, Object>(
                type,searchText,result
        );

//        sorter.setRowFilter(rf);
        sorter.setRowFilter(filter);
    }
    else {
        return;
    }
}//GEN-LAST:event_searchFieldKeyPressed

/**
 * First time, it creates PosterDialog, othervise it just set visibility a set
 * dialog new DVDInfo - creating new entry, not updating old ones
 * 
 * @param evt
 */
private void findFilmItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_findFilmItemActionPerformed
    if (posterDialog == null) {
        posterDialog = new PosterDialog(this, false);
    }
    posterDialog.setInfo( new DVDInfo() );
    posterDialog.setVisible(true);
}//GEN-LAST:event_findFilmItemActionPerformed

private void exitItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitItemActionPerformed
    try {
        this.dbmanager.close();
    } catch (SQLException ex) {
        Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, ex.getMessage(), ex.getSQLState());
    }
    System.exit(0);
}//GEN-LAST:event_exitItemActionPerformed

private void debugWindowItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_debugWindowItemActionPerformed
        if( this.windowLogger == null) {
            windowLogger = new WindowLogger();
        }
        windowLogger.setVisible(true);
        this.logger = Logger.getLogger("Spidex");
        logger.addHandler( WindowLoggerHandler.getInstance() );
}//GEN-LAST:event_debugWindowItemActionPerformed

private void versionItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_versionItemActionPerformed
        if (versionDialog == null) {
            versionDialog = new VersionDialog(this, false);
        }
        versionDialog.setVisible(true);
}//GEN-LAST:event_versionItemActionPerformed

/**
 * Execute import GUI
 * @param evt
 */
private void importItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_importItemActionPerformed
    if(importGUI == null) {
        importGUI = new ImportGUI(this, false);
    }
    importGUI.setVisible(true);
}//GEN-LAST:event_importItemActionPerformed

/**
 * Executed when posterLabel is clicked.Function gets actual info and set to 
 * PosterDialog for updating existing entry
 *  
 * @param evt
 */
private void posterLabelClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_posterLabelClicked
    if (posterDialog == null) {
        posterDialog = new PosterDialog(this, false);
    }
    if(info != null) {
        posterDialog.setInfo(info);
    }
    posterDialog.setVisible(true);
}//GEN-LAST:event_posterLabelClicked

private void eraseItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eraseItemActionPerformed
    int questionResult = JOptionPane.showConfirmDialog( this, 
        ResourceBundle.getBundle("spidex/properties").getString("clearQuestion")
    );

    if(questionResult >= 1) {
       return;
    }

    int[] selectedRows = table.getSelectedRows();
    Arrays.sort(selectedRows);
    for(int rowID=selectedRows.length-1; rowID >= 0; rowID--) {
        this.eraseRecord(selectedRows[rowID]);
    }
}//GEN-LAST:event_eraseItemActionPerformed

private void selectAllItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectAllItemActionPerformed
    this.table.selectAll();
}//GEN-LAST:event_selectAllItemActionPerformed


/**
 * Function get selected items and update its icon in table
 * @param evt Event of action
 */
private void updatePosterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updatePosterActionPerformed
        int[] selectedRows = table.getSelectedRows();
        int row;
        DVDInfo infoItem;
        ImageIcon poster;
        
        Arrays.sort(selectedRows);
        for(int rowID=selectedRows.length-1; rowID >= 0; rowID--) {
            
            row = table.convertRowIndexToModel(selectedRows[rowID]);
            infoItem = this.result.get(row);
            poster = ImageResizer.getInstance().resize( infoItem.getPoster() );           
            this.model.setValueAt(poster, row, 1);
            
        }
}//GEN-LAST:event_updatePosterActionPerformed
           
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUI().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox borrowCheckBox;
    private javax.swing.JTextField borrowesToField;
    private javax.swing.JTextField castField;
    private javax.swing.JLabel castLabel;
    private javax.swing.JButton clearButton;
    private javax.swing.JButton clearSearchFieldButton;
    private javax.swing.JTextField compilationField;
    private javax.swing.JLabel compilationLabel;
    private javax.swing.JMenuItem debugWindowItem;
    private javax.swing.JTextField directorField;
    private javax.swing.JLabel directorLabel;
    private javax.swing.JMenuItem eraseItem;
    private javax.swing.JMenuItem exitItem;
    private javax.swing.JFileChooser fileChooser;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JLabel filterLabel;
    private javax.swing.JComboBox filterTypeComboBox;
    private javax.swing.JMenuItem findFilmItem;
    private javax.swing.JTextField genreField;
    private javax.swing.JLabel genreLabel;
    private javax.swing.JMenuItem importItem;
    private javax.swing.JScrollPane infoPane;
    private javax.swing.JTextArea infoTextArea;
    private javax.swing.JCheckBox lendCheckBox;
    private javax.swing.JTextField lendFromField;
    private javax.swing.JTextField lengthField;
    private javax.swing.JLabel lengthLabel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JTextField nameField;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JSeparator popUpSeparator;
    private javax.swing.JLabel posterLabel;
    private javax.swing.JTextField ratingField;
    private javax.swing.JLabel ratingLabel;
    private javax.swing.JButton saveButton;
    private javax.swing.JScrollPane scrollPane;
    private javax.swing.JTextField searchField;
    private javax.swing.JMenuItem selectAllItem;
    private javax.swing.JSeparator separator;
    private javax.swing.JMenu settingsItem;
    private javax.swing.JTable table;
    private javax.swing.JPopupMenu tablePopup;
    private javax.swing.JComboBox typeComboBox;
    private javax.swing.JLabel typeLabel;
    private javax.swing.JMenuItem updatePoster;
    private javax.swing.JMenuItem versionItem;
    private javax.swing.JTextField yearField;
    private javax.swing.JLabel yearLabel;
    // End of variables declaration//GEN-END:variables
    private PosterDialog posterDialog;
    private VersionDialog versionDialog;
    private DVDInfo info = new DVDInfo();
    private Map<String,String> userSettings;
    
    private WindowLogger windowLogger = new WindowLogger();
    private Logger logger = Logger.getLogger("Spidex");
    private DBManager dbmanager = new DBSQLite();
    private DefaultTableModel model = DVDInfoTableModel.getInstance().getModel();
    TableRowSorter<DefaultTableModel> sorter;
    private List<DVDInfo> result=null;
    private ImportGUI importGUI;
    private Vector<String> columns = ColumnType.getStringArray();
}
